package weather.language.extractor;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.util.ArrayList;

import junit.framework.Assert;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class DateExtractorTest 
{
	private static ArrayList<String> testedSentence;
	private static ArrayList<ArrayList<String>> expectedValues;
	private static final String path = "testsData\\DateExtractor\\";
	private static final String inputFilePattern = "InputSentences";
	private static final String expectedValuesPattern = "ExpectedValues";
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception 
	{
		File f = new File(path);
		String[] inFiles;
		inFiles = f.list(new TestFilesFilter(inputFilePattern));
		
		testedSentence = new ArrayList<String>();
		expectedValues = new ArrayList<ArrayList<String>>();
		for(int i=0;i<inFiles.length;i++)
		{
			String fileName = path + inFiles[i];
			BufferedReader we = new BufferedReader(new FileReader(fileName));
			String line;
			while((line = we.readLine())!=null)
			{
				testedSentence.add(line);
			}
			String[] tokens = inFiles[i].split(inputFilePattern);
			String expectedValuesFileName = path+expectedValuesPattern;
			if(tokens.length == 2)
				expectedValuesFileName+=tokens[1];
			else if(tokens.length>2)
				fail("There is wrong name of file "+fileName);
			System.out.println(expectedValuesFileName);
			
			
			we = new BufferedReader(new FileReader(expectedValuesFileName));
			while((line = we.readLine())!=null)
			{
				ArrayList<String> expectedValTemp = new ArrayList<String>();
				String[] values = line.split("\\|");
				for(int k =0; k<values.length;k++)
				{
					expectedValTemp.add(values[k]);
				}
				expectedValues.add(expectedValTemp);
			}
		}
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception 
	{
	}
	@Test
	public void testExtractValues() throws Exception 
	{
		DateExtractor de = new DateExtractor();
		for(int i =0; i<testedSentence.size();i++)
		{
			String[] dates = de.extractValues(testedSentence.get(i));
			ArrayList<String> expectedDates = expectedValues.get(i);
			Assert.assertNotNull("There is a null value expected Date", expectedDates);
			Assert.assertEquals("Expected "+expectedDates.size()+" dates, but was "+dates.length +" in sentence: "+testedSentence.get(i),dates.length, expectedDates.size());
			for(int k =0; k<dates.length;k++)
			{
				Assert.assertEquals("Expected date is "+expectedDates.get(k)+", but was "+dates[k]+" in sentence"+ testedSentence.get(i),expectedDates.get(k), dates[k]);
			}
		}
	}

	@Test
	public void testExtractAndTag() {
		//fail("Not yet implemented");
	}
	
	

	
}
class TestFilesFilter implements FilenameFilter
    {
		
		private String start ="";
		public TestFilesFilter(String start)
		{
			this.start = start;
		}
		public boolean accept(File dir, String s)
		{
			if ( s.startsWith(start) ) 
			{
				return true;
			}
			else
				return false;
		}
    }
